package com.example.myapplication.User;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


import com.example.myapplication.AbsFragment.Home;
import com.example.myapplication.Adapter.MyViewPagerAdapter;
import com.example.myapplication.Data.UserData;
import com.example.myapplication.R;

import com.example.myapplication.Student.StudentCourse;
import com.example.myapplication.Teacher.TeacherCourse;

import java.util.ArrayList;
import java.util.List;

public class UserActivity extends AppCompatActivity
        implements View.OnClickListener,ViewPager.OnPageChangeListener{

    private ViewPager viewPager;
    private TextView textView;
    private ImageView imageView;
    private RadioGroup radioGroup;
    private RadioButton button_1;
    private RadioButton button_2;
    private RadioButton button_3;
    private Home home;
    private Home course;
    private Home personal;

    private ListView lvCourseList;

    UserData userData;
    private int roleId;
    private List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_navigationbar);

        //初始化
        initView();
    }

    //初始化
    private void initView() {
        //初始化部件
        lvCourseList = findViewById(R.id.list_courses);
        textView = findViewById(R.id.tvi_home);

        //获取用户数据
        userData = (UserData) getIntent().getSerializableExtra("userData");
        roleId = userData.getRoleId();

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        //找到3个按钮
        button_1 = (RadioButton) findViewById(R.id.button_1);
        button_2 = (RadioButton) findViewById(R.id.button_2);
        button_3 = (RadioButton) findViewById(R.id.button_3);

        //判断身份，并创建Fragment对象及集合
        if(roleId == 0) {
            home = new UserHome();
            course = new StudentCourse();
            personal = new UserPersonal();
        } else if(roleId == 1){
            home = new UserHome();
            course = new TeacherCourse();
            personal = new UserPersonal();

        } else {
            Log.d("chenpengyu--Error","身份判断错误，Fragment对象无法创建");
        }

        //将Fragment对象添加到list中
        list = new ArrayList<>();
        list.add(home);
        list.add(course);
        list.add(personal);

        //给viewPager设置适配器，以显示内容
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);

        //设置RadioGroup开始时设置的按钮，设置第一个按钮为默认值
        radioGroup.check(R.id.button_1);
        //设置Viewpager第一次显示的页面
        viewPager.setCurrentItem(0,true);

        //设置按钮点击监听
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);
        button_3.setOnClickListener(this);

        //设置ViewPager页面监听，实现左右滑动
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void finish() {
        ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
        viewGroup.removeAllViews();
        super.finish();
    }

    //处理点击的方法
    @Override
    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.button_1) {
            viewPager.setCurrentItem(0, true);
        } else if (id == R.id.button_2) {
            viewPager.setCurrentItem(1, true);
        }else if (id == R.id.button_3) {
            viewPager.setCurrentItem(2, true);
        }

    }

    //处理页面变化的方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    //用于页面切换监听的回调方法
    @Override
    public void onPageSelected(int position) {

        switch (position) {
            case 0:
                radioGroup.check(R.id.button_1);
                textView.setText("全部课程");
                break;
            case 1:
                radioGroup.check(R.id.button_2);
                textView.setText("我的课程");
                break;
            case 2:
                radioGroup.check(R.id.button_3);
                textView.setText("个人主页");
                break;
            default:
                break;

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
